import Vuex from "vuex"
import Vue from "vue"
import api from "@/api"
import { AppIdEnum } from "@/constant"
import storage from "@/utils/storage"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    topbar: [{}],
    userInfo: {},
    token: "",

    wallpaper:
      "https://s3-lc.thelarkcloud.com/obj/larkcloud-mgcloud/baas/qc4nsi/1c9595c9e34b67d1_1629532355758.jpeg",

    drawerCollects: [],
    desktopCollects: [],
    showDesktop: true,
    apps: [
      {
        appId: AppIdEnum.COLLECT_MODAL,

        width: 400,
        height: 380,
        zIndex: 50,
        // href: '//www.bilibili.com/',
        dock: true,
        hidden: true,
        title: "更新收藏",
        compName: "addCollect",
        open: false, //是否打开
        min: false, //最小化
        max: false //最大化
      },
      {
        appId: AppIdEnum.LOGIN,

        width: 380,
        height: 500,
        zIndex: 50,
        dock: true,
        hidden: true,
        title: "",
        compName: "login",
        open: false, //是否打开
        min: false, //最小化
        max: false //最大化
      },
      {
        appId: AppIdEnum.ABOUT,
        width: 1000,
        height: 600,
        zIndex: 50,
        dock: true,
        hidden: true,
        title: "关于我",
        compName: "about",
        open: false, //是否打开
        min: false, //最小化
        max: false //最大化
      },
      {
        appId: AppIdEnum.TODOLIST,
        icon: "https://s6-lc.thelarkcloud.com/obj/larkcloud-mgcloud/baas/qc4nsi/403db5899589d3a4_1629592032221.png",
        width: 400,
        height: 600,
        zIndex: 50,
        dock: true,
        hidden: false,
        title: "待办事项",
        compName: "todoList",
        open: false, //是否打开
        min: false, //最小化
        max: false //最大化
      },
      {
        appId: AppIdEnum.WALLPAPER,
        icon: "https://s6-lc.thelarkcloud.com/obj/larkcloud-mgcloud/baas/qc4nsi/ff770c3865564f67_1629697083593.png",
        width: 500,
        height: 500,
        zIndex: 50,
        dock: true,
        hidden: false,
        title: "壁纸更换",
        compName: "wallpaper",
        open: false, //是否打开
        min: false, //最小化
        max: false //最大化
      },
      {
        appId: AppIdEnum.NOTEPAD,
        icon: "https://s1-lc.thelarkcloud.com/obj/larkcloud-mgcloud/baas/qc4nsi/fbf09429c0cf9758_1629592024656.png",
        width: 500,
        height: 600,
        zIndex: 50,
        dock: true,
        hidden: false,
        title: "记事本",
        compName: "NotePad",
        open: false, //是否打开
        min: false, //最小化
        max: false //最大化
      },
      {
        appId: AppIdEnum.DRAWER,
        icon: "https://s3-lc.thelarkcloud.com/obj/larkcloud-mgcloud/baas/qc4nsi/e7b8e52c4f7ced48_1629615045389.png",
        width: 288,
        height: 450,
        zIndex: 50,
        dock: true,
        hidden: false,
        title: "抽屉",
        compName: "DrawerPanel",
        open: false, //是否打开
        min: false, //最小化
        max: false //最大化
      },
      {
        appId: AppIdEnum.UPDATEUSER,
        width: 500,
        height: 800,
        zIndex: 50,
        dock: true,
        hidden: true,
        title: "修改个人信息",
        compName: "updateUser",
        open: false, //是否打开
        min: false, //最小化
        max: false //最大化
      },
      {
        appId: AppIdEnum.PERSAYING,
        icon: "https://s6-lc.thelarkcloud.com/obj/larkcloud-mgcloud/baas/qc4nsi/0428b0049683172f_1629644369933.png",
        width: 360,
        height: 500,
        zIndex: 50,
        dock: true,
        hidden: false,
        title: "好诗词",
        compName: "perSaying",
        open: false, //是否打开
        min: false, //最小化
        max: false //最大化
      }
    ],
    activeAppId: "",
    currentZIndex: 50
  },
  getters: {
    dockApps: (state) => {
      return state.apps.filter((item) => item.dock === true)
    },
    linkApps: (state) => {
      return state.apps.filter((item) => item.dock === false)
    },
    isLogin: (state) => {
      return !!state.token
    }
  },
  actions: {
    restoreWallpaper({ state }) {
      const wallpaper = storage.getItem("current-wallpaper")
      if (wallpaper) {
        state.wallpaper = storage.getItem("current-wallpaper")
      }
    },
    changeWallpaper({ state }, src) {
      state.wallpaper = src
      storage.setItem("current-wallpaper", src)
    },
    updateUserInfo({ state }, data) {
      state.userInfo = data
    },
    updateToken({ state }, token) {
      state.token = token
    },

    getUserInfo({ dispatch, state }) {
      const token = storage.getItem("token")
      state.token = token
      if (token) {
        api.getUserInfo().then((result) => {
          dispatch("updateUserInfo", result.data.user)
        })
      }
    },

    logout({ state }) {
      storage.removeItem("token")
      state.token = ""
      state.userInfo = {}
    },
    addApp({ state }, app) {
      state.apps.push(app)
    },
    getCollects({ state }) {
      return api
        .getCollects()
        .then(({ data }) => {
          state.desktopCollects = data.data.deskTop
          state.drawerCollects = data.data.inDrawer
        })
        .catch((error) => {
          console.error(error)
        })
    },

    delCollect({ dispatch }, data) {
      return dispatch("authJudge").then(() => {
        return api.delCollect(data._id).then((result) => {
          if (result.data.status == "ok") {
            //哪个有就删哪个
            dispatch("delFromDesk", data._id)
            dispatch("delFromDrawer", data._id)
          }
        })
      })
    },

    delFromDesk({ state }, id) {
      const index = state.desktopCollects.findIndex((item) => item._id == id)
      if (index !== -1) {
        state.desktopCollects.splice(index, 1)
      }
    },
    delFromDrawer({ state }, id) {
      const index = state.drawerCollects.findIndex((item) => item._id == id)
      if (index !== -1) {
        state.drawerCollects.splice(index, 1)
      }
    },
    moveToDrawer({ state, dispatch }, data) {
      dispatch("authJudge").then(() => {
        //调用移动接口更新
        api.moveToDrawerOrDesk(data._id, true).then(() => {
          dispatch("delFromDesk", data._id)
          state.drawerCollects.push(data)
        })
      })
    },

    moveToDesk({ state, dispatch }, data) {
      console.log("移动到桌面")
      dispatch("authJudge").then(() => {
        //调用移动接口更新
        api.moveToDrawerOrDesk(data._id, false).then(() => {
          dispatch("delFromDrawer", data._id)
          state.desktopCollects.push(data)
        })
      })
    },

    //拖拽排序之后调用
    updateDeskTopCollects({ state }, collects) {
      //暂未提供
      console.log(state, collects)
      // return request.post('', {
      //   ...state.userInfo,
      //   collects
      //  }).then((result) => {
      //    console.log(result)
      //  }).catch(error => {
      //    console.error(error)
      //  })
    },
    saveCollect({ dispatch }, data) {
      //添加或更新收藏链接

      return dispatch("authJudge").then(() => {
        return api.getWebIconAnySize(data.webSite).then((result) => {
          return api.saveCollect({
            ...data,
            webIco: result.data
          })
        })
      })
    },

    authJudge({ dispatch }) {
      return new Promise((resovle, reject) => {
        const token = storage.getItem("token")
        // console.log("token", token)

        if (token) {
          resovle()
        } else {
          dispatch("openApp", AppIdEnum.LOGIN)
          reject("请登录")
        }
      })
    },

    syncApps({ state }, apps) {
      //上传app列表方法
      if (storage.getItem("token")) {
        //登录之后才会同步到远程
        //不登录不同步
        console.log(state)
        const data = apps.map((item) => {
          return {
            webId: item._id,
            webOrder: item.webOrder
          }
        })
        api.updateCollectOrder(data)
      }
    },
    setCurrentASpp({ state }, appId) {
      state.activeAppId = appId
      const app = state.apps.find((item) => item.appId === appId)
      if (app) {
        state.currentZIndex += 1
        app.zIndex = state.currentZIndex
      }
    },
    minApp({ state }, appId) {
      const app = state.apps.find((item) => item.appId === appId)
      if (app) {
        app.min = true
      }
    },
    toggleMaxApp({ state }, { appId, isMax }) {
      const app = state.apps.find((item) => item.appId === appId)
      if (app) {
        app.max = !isMax
      }
    },
    closeApp({ state }, appId) {
      const app = state.apps.find((item) => item.appId === appId)
      if (app) {
        app.open = false
      }
    },
    openApp({ state }, appId) {
      const app = state.apps.find((item) => item.appId === appId)
      if (app) {
        app.open = true
        app.min = false
        state.activeAppId = appId
        state.currentZIndex += 1
        app.zIndex = state.currentZIndex
      }
    },
    changeShowDesktop({ state }, showDesktop) {
      state.showDesktop = showDesktop
    }
  }
})
